//
//  playMusicViewController.m
//  B项目
//
//  Created by lanou on 16/8/9.
//  Copyright © 2016年 Work. All rights reserved.
//

#import "playMusicViewController.h"
#import "PrefixHeader.pch"
#import "musicManager.h"
#import "PlayerModel.h"
@interface playMusicViewController ()

//scrollView
@property(nonatomic,strong)UIScrollView *scrollView;

//图片
@property(nonatomic,strong)UIImageView *songPic;

//开始时间
@property(nonatomic,strong)UILabel *startLabel;

//结束时间
@property(nonatomic,strong)UILabel *endLabel;

//歌曲名
@property(nonatomic,strong)UILabel *songNameLabel;

//歌手名
@property(nonatomic,strong)UILabel *authorNameLabel;

//进度条
@property(nonatomic,strong)UISlider *slider;



//定时器
@property(nonatomic,strong)NSTimer *timer;



//歌词时间数组和对应歌词数组
@property(nonatomic,strong)NSArray *timeArray;
@property(nonatomic,strong)NSArray *lrcArray;

@end


@implementation playMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //模糊视图
    UIImage *backImg = [UIImage imageNamed:@"11471923,2560,1600.jpg"];
    UIImageView *imgV = [[UIImageView alloc]initWithImage:backImg];
    [self.view addSubview:imgV];

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)];
    UIVisualEffectView *effectivew = [[UIVisualEffectView alloc]initWithEffect:blur];
    effectivew.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [imgV addSubview:effectivew];

    [self creatScrollView];
    [self playControl];
     [[musicManager shareInstance]loadSongInfo];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadDataWithModel) name:@"loadData" object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(startPlayMusic) name:@"readToPlay" object:nil];
    [musicManager shareInstance].playStyle = 0;
    self.slider.value = 0;
    //定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    [self.timer fire];




}


//创建scrollView等
- (void)creatScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth, kScreenWidth)];

    _scrollView.contentSize = CGSizeMake(kScreenWidth *2, kScreenWidth);
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;

    //播放图片
    self.songPic = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth*0.1, kScreenWidth*0.1, kScreenWidth*0.8, kScreenWidth*0.8)];
    _songPic.layer.cornerRadius = self.songPic.bounds.size.width/2;
    _songPic.layer.masksToBounds = YES;
    _songPic.layer.borderColor = [[UIColor whiteColor]CGColor];



    //返回按钮
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(20, 20, 40, 40);
    [button setImage:[UIImage imageNamed:@"arrowdown.png"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(btAction) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:_scrollView];
    [self.scrollView addSubview:_songPic];
    [self.scrollView addSubview:button];

}

//播放的操作空间
- (void)playControl
{
    //歌曲名
    self.songNameLabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth -150)/2, _scrollView.endY + 10, 150, 20)];
    _songNameLabel.textColor = [UIColor whiteColor];
    _songNameLabel.textAlignment = NSTextAlignmentCenter;

    //歌手
    self.authorNameLabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth -150)/2, _songNameLabel.endY + 10, 150, 20)];
    _authorNameLabel.textColor = [UIColor whiteColor];
    _authorNameLabel.textAlignment = NSTextAlignmentCenter;


    //开始时间
    self.startLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, _authorNameLabel.endY + 20, 50, 20)];
    _startLabel.textColor = [UIColor whiteColor];
    _startLabel.text = @"00:00";
    _startLabel.textAlignment = NSTextAlignmentCenter;

    //结束时间
    self.endLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 65, _authorNameLabel.endY + 20, 50, 20)];
    _endLabel.text = @"00:00";
    _endLabel.textColor = [UIColor whiteColor];
    _endLabel.textAlignment = NSTextAlignmentCenter;

    //进度条
    self.slider = [[UISlider alloc]initWithFrame:CGRectMake(15, _startLabel.endY + 20, kScreenWidth - 30, 30)];
    [_slider  addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];

    [self.view addSubview:_songNameLabel];
    [self.view addSubview:_authorNameLabel];
    [self.view addSubview:_startLabel];
    [self.view addSubview:_endLabel];
    [self.view addSubview:_slider];

    NSArray *btArray = @[@"bt_playpagen_control_round-all_normal",@"player_prev",@"player_play",@"player_next",@"player_download"];
    CGFloat btWidth = (kScreenWidth - 180)/5;
    CGFloat margin = 30.0;

    for (NSInteger i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake((margin + btWidth)*i + margin, _slider.endY + 40, btWidth, btWidth);
        button.tag = 100 +i;
        [button setImage:[UIImage imageNamed:btArray[i]] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:button];

    }



}


- (void)reloadDataWithModel
{
    PlayerModel *model = [musicManager shareInstance].model;

    dispatch_async(dispatch_get_main_queue(), ^{
    [self.songPic sd_setImageWithURL:[NSURL URLWithString:model.pic_radio] placeholderImage:[UIImage imageNamed:@"ic_blank_collect@3x.png"]];
        self.songNameLabel.text = model.title;
        self.authorNameLabel.text = model.author;
    });


}

//开始播放歌曲 切换图标
- (void)startPlayMusic
{
      UIButton *button = (UIButton *)[self.view viewWithTag:102];
        if ([[musicManager shareInstance]readToPlay] == YES) {

            [button setImage:[UIImage imageNamed:@"player_pause@2x"] forState:(UIControlStateNormal)];
        }else{
            [button setImage:[UIImage imageNamed:@"player_play@2x.png"] forState:(UIControlStateNormal)];
        }





}

//进度条实现方法

- (void)sliderAction:(UISlider *)slider
{

    [[musicManager shareInstance]playerProgressWithProgressFloat:slider.value];


}


//按钮点击事件
- (void)buttonClick:(UIButton *)button
{

    if (button.tag == 100) {
        //播放模式
        [self playStyle:button];
    }else if (button.tag == 101){
        //播放上一首
        [[musicManager shareInstance] playerAbover];
        
    }else if (button.tag == 102){
        //播放和暂停
        [self playOrPause:button];

    }else if (button.tag == 103){
        //下一首
        [[musicManager shareInstance]playerNext];
    }else{
        //下载
    }

}

//暂停/播放
- (void)playOrPause:(UIButton *)button
{
    if ([musicManager shareInstance].readToPlay == YES) {
        [[musicManager shareInstance] pauseMusic];
        [button setImage:[UIImage imageNamed:@"player_play@2x"] forState:(UIControlStateNormal)];
        [musicManager shareInstance].readToPlay = NO;
    }else{
        [musicManager shareInstance].readToPlay = YES;
        [[musicManager shareInstance] startPlayMusic];
        [button setImage:[UIImage imageNamed:@"player_pause@2x"] forState:(UIControlStateNormal)];
    }
}


//播放模式
- (void)playStyle:(UIButton *)button
{
    NSArray *arr = @[@"bt_playpagen_control_round-all_normal",@"bt_playpagen_control_round-one_normal",@"bt_playpagen_control_random_normal",@"bt_playpagen_control_order_normal"];
    if ([musicManager shareInstance].playStyle == 3) {
        [musicManager shareInstance].playStyle = 0;
    }else{
        [musicManager shareInstance].playStyle = [musicManager shareInstance].playStyle + 1;
    }

    [button setImage:[UIImage imageNamed:arr[[musicManager shareInstance].playStyle]] forState:(UIControlStateNormal)];
}


//定时器
- (void)timeAction
{
    if ([musicManager shareInstance].player.currentTime.timescale == 0 || [musicManager shareInstance].player.currentItem.duration.timescale == 0) {
        return ;
    }

    long long int currentTime = [musicManager shareInstance].player.currentTime.value/[musicManager shareInstance].player.currentTime.timescale;
    self.startLabel.text = [NSString stringWithFormat:@"%02lld:%02lld",currentTime/60,currentTime%60];
    //获得总时长/
    long long int allTime = [musicManager shareInstance].player.currentItem.duration.value/[musicManager shareInstance].player.currentItem.duration.timescale;
    self.endLabel.text = [NSString stringWithFormat:@"%02lld:%02lld",allTime/60,allTime%60];
    self.slider.maximumValue = allTime;
    self.slider.value = currentTime;

    if (self.slider.value >= allTime) {
        [[musicManager shareInstance]playerNext];
    }
    if ([musicManager shareInstance].player.rate == 1.0) {

        [UIView beginAnimations:@"rotation" context:nil];

        [UIView setAnimationDuration:1];

        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.songPic.transform = CGAffineTransformRotate(self.songPic.transform, 0.5);
        [UIView commitAnimations];
    }

}





//播放返回
- (void)btAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
